



////////////////////////////////////////////////
// IV LONG RUN ANALYSIS BIRTH YEAR <=1994    ///
////////////////////////////////////////////////



use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv_long", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions




////////////////////////////////////////////////
//   time=[17-25]                             //
////////////////////////////////////////////////




keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)


tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv_long"





keep if _merge==3
drop _merge




encode(edu_type_m1), gen(primary_school)


/* Code country of origin */

gen race=.
replace race=1 if ie_type_m1==1 // Danish
replace race=2 if ie_type_m1==2 // Foreign
replace race=3 if ie_type_m1==. // Missing

tab race, gen(racedum)



// Persons in the house 


gen house_number=.

replace house_number=1 if antpersf_d_m1==1
replace house_number=2 if antpersf_d_m1==2
replace house_number=3 if antpersf_d_m1==3
replace house_number=4 if antpersf_d_m1>3 & antpersf_d_m1!=.
replace house_number=5 if antpersf_d_m1==.


tab house_number, gen(housedum)


gen housing_missing=1 if house_number==5
replace housing_missing=0 if house_number!=5


// age 
gen age_0_40=(age0>=1 & age0<=40 & age0!=.)
gen age_41_50=(age0>=41 & age0<=50 & age0!=.)
gen age_51_60=(age0>=51 & age0<=60 & age0!=.)
gen age_61_70=(age0>=61 & age0<=70 & age0!=.)


// Attrition

gen attrition=0
replace attrition=1 if formue_real==.


// Selfemployed

gen selfemployed=1 if netovskud_real!=0 & netovskud_real!=.
replace selfemployed=0 if netovskud_real==0 & netovskud_real!=.


// Education edu_type_m1

encode(edu_type_m1), gen(edu_prior)
gen lower_secondary=(edu_prior<=2 & edu_prior!=.)
gen higher_secondary=(edu_prior==3 & edu_prior!=.)
gen university=(edu_prior>3 & edu_prior!=.)
gen edu_missing=(edu_prior==.)


// Children in the home


gen children_inhouse=1 if antboernf_m1>0 & antboernf_m1!=.
replace children_inhouse=0 if antboernf_m1==0 
replace children_inhouse=0 if antboernf_m1==. 

gen children_missing=0
replace children_missing=1 if antboernf_m1==.



// Wage 

egen timelon_groups=xtile(timelon_real_m1), nq(4) by(pyear)

replace timelon_groups=5 if timelon_groups==.  // category for missing

tab timelon_groups, gen(timelon_dum)



// use means



local sumvar "erhvervsindk employed_pstill unemployed_pstill social  legmarried house passiv aktiv formue"



foreach var in `sumvar'  {

egen `var'_mean=rowmean(`var'_m1 `var'_m2 `var'_m3 `var'_m4)

}







keep if time>=17 & time<=25
keep if pyear<=1994

tab court, gen(courtdum)

tab time, gen(timedum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab aar, gen(aardum)
drop aardum1




local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real bankgaeld_real"



tab time


/* IV Regress */



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==17

local individuals=e(N)	
 

sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean) 


ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum* , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



outreg2 using iv_results_17_25_balanced.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',individuals, `individuals')



foreach var of varlist `depvar'{
	
reg   `var' granted `Xvar_short' i.court_pyear if time==17
local individuals=e(N)	

sum `var' if granted==0
local dismissed_mean=r(mean)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum*, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_17_25_balanced.xls, append ctitle(IV `var')  


}




/* Hourly wage */



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"

reg timelon_real granted `Xvar_short' i.court_pyear if time==17
local individuals=e(N)	

sum timelon_real if granted==0
local dismissed_mean=r(mean)




ivregress 2sls timelon_real  `Xvar_short' i.court_pyear (granted=iv_lawyer2) aardum*, cluster(lawyer_id_unique) robust


outreg2 using iv_results_17_25_balanced.xls, append ctitle(IV timelon_real)  







